package com.ruoyi.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.jm.CertificateInfo;
import com.ruoyi.common.utils.jm.ESSCertificate;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.service.IPdfTokenService;
import com.ruoyi.system.service.ISealJurService;
import com.ruoyi.web.dto.SealDto;
import com.ruoyi.web.service.IWebSignService;
import com.ruoyi.web.service.SealService;
import com.ruoyi.web.util.WebSign;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.security.cert.CertificateException;
import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.utils.Base64Utils.ESSGetBase64Decode;
import static com.ruoyi.common.utils.Base64Utils.ESSGetBase64Encode;
import static com.ruoyi.common.utils.StringUtils.isNull;
import static com.ruoyi.web.util.ImageUtil.markImageBySingleText;

@Controller
@RequestMapping(value="/web")
@CrossOrigin
public class WebSignController {

    @Resource
    private ISealJurService sealJurService;
    @Resource
    private SealService sealService;
    @Resource
    private IWebSignService webService;
    @Resource
    private IPdfTokenService pdfTokenService;

    public String errImg ="R0lGODlhfgB8APcAAAAAAAAAMwAAZgAAmQAAzAAA/wArAAArMwArZgArmQArzAAr/wBVAABVMwBVZgBVmQBVzABV/wCAAACAMwCAZgCAmQCAzACA/wCqAACqMwCqZgCqmQCqzACq/wDVAADVMwDVZgDVmQDVzADV/wD/AAD/MwD/ZgD/mQD/zAD//zMAADMAMzMAZjMAmTMAzDMA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dzF63zK+dQVzwzNq2zL00zN5TzNgYzEXRzPKoDKX0EP4RzP5OvFtZzB93zQ9dzJ8uy2/WykJLEMuLzQiHzEFF3QFm3Jt1zR/xktsyzcbe5JS4fcyxK9yvlc0QrNyvScvyMd0bj8HPRVDyItz+vczil90jad0it9yb2sAirgs10BDSvA0zk90BNt0spc1EMtxBzdz4wnEGEgxCGd1Ec9zvNM0xu9zUrtzk3t1DEd1VL91WAd0TstxN0cKo1qNJkg1F3N0mHd1tC81ricCaISsl8hx2Mt0jHt1mB912ItxDFAyH8iBmrd133t1Xmt14Wd1XiNyyogBlvNEHad2Cw91oqd03zt1ZOd2SG9Ag792JLA01Ed2po92qIt2llN2okN2mX92MwbBoNd2mxt2JU92pJ92YytAoywa8dC13jRXMSwBa+d2rIN2//DndrCHQZoy9vPMSrmRw+MOLLAHdzFTdzGXdilzdNbQDQh4dwVsais7d05mWn0sAz0oAzljXTEsAigHdLsTd3uzdhi3d6gvQhEY97nXQ/KUA8hsQyuWjq6/BW73d/5bd76bRH04A7K8BEHLglVsN6hrdYQ3t63Hd883eDsvQWT4A4HzhMajuBIpw/k3d9U4tPhUzqu2i7cjd/nbd7E0A4tjqxT0QuMEN3rfdvsDd83XuPyXQV20Q4u7hHDQAwebt72jd/PjUADfCCam2kqbt4I7g4uHuUwnhGqwR0zbuNYHuE5vt5V4BKp8RZUDuNQrgzt8OTjjXSZxt//TRIdst1mmWY4513eCv7i7kDlb+Ekm6ElNwHcNu7gDr4Cp/yDF7MaGTEVQG7m9k3e5r2oWpHkAD7Xzo105P3kyVDnlr4UmUEeMMM/MBGWhvkHbQZZyJMwec4WTuEULZ7q7ZDgBh7pgOvoEREQADs=";

    /**
     * 网页签章接口（暂时只支持手写签名签章）
//     * @param sysId 业务系统id
//     * @param userId 手签人标识，可以是江苏省统一人员id，也可以是系统内人员的身份证号。
//     * @param plainText 需要签名的明文信息
//     * @param plainEncodeType 签名编码格式
     * @return JSON字符串
     */
    @RequestMapping(value ="/hnzy/webSign")
    @ResponseBody
    public String WebServerSign(String data, HttpServletRequest request){
        data = complementParam(data,request);
        //新建json对象，作为返回信息
        JSONObject jsonObject = new JSONObject();
        //首先确认参数
        JSONObject dataJs = null;
        try{
            dataJs = JSONObject.parseObject(data);
        }catch (Exception e){
            jsonObject = new JSONObject();
            jsonObject.put("code","0001");
            jsonObject.put("msg","参数格式异常");
            return jsonObject.toJSONString();
        }
        String sysId = dataJs.getString("sysId");
        String userId = dataJs.getString("userId");
        String plainText = dataJs.getString("plainText");
        String encodeType = dataJs.getString("encodeType");

        //签章序列号 每次签章的序号，取uuid;保证唯一性
        String signSerialNum = IdUtils.simpleUUID();
        //根据身份证号查询印章数据
        SealDto sealDto = sealService.findSealBySysPersonId(userId,sysId);
        if (sealDto==null){
            //如果印章数据为空
            jsonObject.put("code","0002");
            jsonObject.put("msg","签名不存在");
            return jsonObject.toString();
        }
        //获取图像字符节
        byte[] bGif = ESSGetBase64Decode(sealDto.getSeal_img());
        //获取证书字符节
        byte[] bPfx = ESSGetBase64Decode(sealDto.getSeal_pfx());
        byte[] bCert = ESSGetBase64Decode(sealDto.getSeal_cert());
        //获取证书密码
        String pfxPwd = sealDto.getSeal_pwd();
        //签名方法
        WebSign ws = new WebSign();
        try {
            if(ws.SignData(plainText, encodeType,bPfx, pfxPwd,bGif)) {
                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("encodeData",ws.EncodedData);
                jsonObject1.put("imageData",ws.ImageData);
                jsonObject1.put("ImageID",ws.ImageID);

                jsonObject.put("code","0000");
                jsonObject.put("msg","签章成功");
                jsonObject.put("data",jsonObject1);
            }else{
                jsonObject.put("code","0001");
                jsonObject.put("msg","签名过程异常");
            }
        } catch (IOException | GeneralSecurityException | CertificateException e) {
            jsonObject.put("code","0003");
            jsonObject.put("msg","签名异常");
            return jsonObject.toString();
        }
        return jsonObject.toString();
    }

    @RequestMapping(value ="/hnzy/webVerify")
    @ResponseBody
    public String WebServerHandWritingVerify(String data, HttpServletRequest request){
        data = complementParam(data,request);
        //新建json对象，作为返回信息
        JSONObject jsonObject = new JSONObject();
        //首先确认参数
        JSONObject dataJs = null;
        try{
            dataJs = JSONObject.parseObject(data);
        }catch (Exception e){
            jsonObject = new JSONObject();
            jsonObject.put("code","0001");
            jsonObject.put("msg","参数格式异常");
            return jsonObject.toJSONString();
        }
        String encodeData = dataJs.getString("encodeData");
        String imageData = dataJs.getString("imageData");
        String plainText = dataJs.getString("plainText");
        String encodeType = dataJs.getString("encodeType");

        JSONObject result = new JSONObject();
        WebSign ws = new WebSign();
        if (!"".equals(encodeData)&&!"".equals(imageData)&&!"".equals(plainText)){
            if(ws.VerifyData(plainText, encodeType,encodeData, imageData)||ws.VerifyDataSM2(plainText, encodeType,encodeData, imageData)) {
                result.put("code","0000");
                result.put("msg","验证成功");
                JSONObject js = new JSONObject();
                js.put("gifBase64",ws.PictureData);
                js.put("signTime",ws.SealTime);
                result.put("data",js);
            }else{
                try {
                    result.put("code","0012");
                    result.put("msg","验证失败");
                    JSONObject js = new JSONObject();
                    if("".equals(ws.PictureData)){
                        js.put("gifBase64",errImg);
                    }else{
                        js.put("gifBase64",addMarkErrorText(ws.PictureData));
                    }
                    result.put("data",js);
                } catch (IOException e) {
                    result.put("resultType","false");
                    result.put("errorCode","10004");
                    result.put("gifBase64",errImg);
                }
            }
        }
        return result.toString();
    }

    private String addMarkErrorText(String data) throws IOException {
        byte[] img = markImageBySingleText(ESSGetBase64Decode(data), Color.green,null,null);
        return ESSGetBase64Encode(img);
    }
    /**
     * 签名接口
     *参数示例
     *     {
     *         "signToken":"634e33ad3ea6475cb6c89c1e783130b8",
     *         "sealId":"022e9c4f36c04c928cd264a62966aed1",
     *         "hashValue":"18f1a21ef4005c3672c0c5febdbba7a4e3c93510",
     *         "parsePatterns":"yyyy/MM/dd HH:mm"
     *     }
     *     结果示例
     *     {
     *         "msg": {
     *              "signTime":"2021/05/11 14:42",
     *              "signCert":"xxxx",
     *              "signValue":"xxxx",
     *              "version":"200",
     *              "sealID":"022e9c4f36c04c928cd264a62966aed1",
     *              "sealType":"ESS001",
     *              "sealStruct":"中办标准",
     *              "sealName":"XXX章",
     *              "hashValue":"18f1a21ef4005c3672c0c5febdbba7a4e3c93510",
     *              "signAlgorithm":"SM2",
     *              "hashAlgorithm":"SM3",
     *              "signSerialNumber":"634e33ad3ea6475cb6c89c1e783130b8"
     *         },
     *         "code": "w0000"
     *     }
     * @return JSON字符串
     */
    @RequestMapping(value ="/sign")
    @ResponseBody
    public String sign(@RequestBody JSONObject jsonObject) {
        JSONObject result = new JSONObject();
        //检查参数
        String signToken ; //
        String sealId ;
        String hashValue ;
        String parsePatterns ;
        String verifyUUID ;
        if (jsonObject.containsKey("signToken")&&jsonObject.containsKey("sealId")&&jsonObject.containsKey("hashValue")
                &&jsonObject.containsKey("parsePatterns")){
            signToken = jsonObject.getString("signToken");
            sealId = jsonObject.getString("sealId");
            hashValue = jsonObject.getString("hashValue");
            parsePatterns = jsonObject.getString("parsePatterns");
//            verifyUUID = jsonObject.getString("verifyUUID");
        }else {
            result.put("code","w0001");
            result.put("msg","参数类型不全");
            return result.toJSONString();
        }
//        if (!pdfTokenService.checkVerifyUUID(signToken,verifyUUID)){
//            result.put("code","w0005");
//            result.put("msg","当前操作未验证，请先验证");
//            return result.toJSONString();
//        }


        String signTime = DateUtils.parseDateToStr(parsePatterns,new Date());
        //拼接
        hashValue = hashValue +signTime;
        SealDto seal = sealService.findSealById(sealId);
        if (seal==null){
            result.put("code","w0002");
            result.put("msg","印章不存在");
            return result.toJSONString();
        }
        if ("1".equals(seal.getSeal_standard())||"2".equals(seal.getSeal_standard())||"4".equals(seal.getSeal_standard())){
            result.put("code","w0003");
            result.put("msg","服务器签名不支持的印章类型");
            return result.toJSONString();
        }
        byte[] signValue =  webService.webSign(seal,hashValue.getBytes());

        if (signValue==null){
            result.put("code","w0004");
            result.put("msg","签名时发生异常");
            return result.toJSONString();
        }
        JSONObject msgJS = new JSONObject();
        msgJS.put("signValue",ESSGetBase64Encode(signValue));
        msgJS.put("signTime",signTime);
        msgJS.put("signCert",seal.getSeal_cert());
        msgJS.put("version","200");
        msgJS.put("sealID",seal.getSealId());
        msgJS.put("sealType",seal.getSeal_type());
        msgJS.put("sealName",seal.getSealName());
        msgJS.put("hashValue",hashValue);
        String sealStandard = seal.getSeal_standard();
        if(sealStandard.equals("1") || sealStandard.equals("2") || sealStandard.equals("3")){
            msgJS.put("signAlgorithm","SM2");
            msgJS.put("hashAlgorithm","SM3");
        }else {
            msgJS.put("signAlgorithm","RSA");
            msgJS.put("hashAlgorithm","SHA1");
        }
        if(sealStandard.equals("4") || sealStandard.equals("5") || sealStandard.equals("6")){
            msgJS.put("sealStruct","公司自定义标准");
        }else if(sealStandard.equals("1") || sealStandard.equals("3") ){
            msgJS.put("sealStruct","安可标准");
        }else if(sealStandard.equals("2")){
            msgJS.put("sealStruct","国办标准");
        }
        msgJS.put("signSerialNumber", IdUtils.simpleUUID());

        result.put("code","w0000");
        result.put("msg",msgJS.toJSONString());

        //签章成功，写签章日志
        //根据签章结果，写入签章日志
        SignLog signatureLog = new SignLog();
        signatureLog.setSealId(sealId);
        signatureLog.setProductType("web");
        signatureLog.setSignResult(0);
        signatureLog.setErrorContent("");
//        logService.insertSignLog(signatureLog);
        return result.toJSONString();
    }
    /**
     * 验证接口
     *    参数示例
     *     {
     *         "signToken":"634e33ad3ea6475cb6c89c1e783130b8",
     *         "sealId":"022e9c4f36c04c928cd264a62966aed1",
     *         "hashValue":"18f1a21ef4005c3672c0c5febdbba7a4e3c93510",
     *         "signTime":"yyyy/MM/dd HH:mm",
     *         "signCert":"XXXXXXXX",
     *         "signedValue":"MEUCIQCcxteR9x+6J5RiOwYcU0Au6cKWlmz7NLSxHqpJ12hBvAIgGkFWXQ/Fiy4lJwYpU0dmVOIYF/85sKe+xuGIYGwWeic="
     *     }
     *     结果示例
     *     {
     *         "msg": "{"certVerify":true,"documentVerify":false}",
     *         "code": "w0000"
     *     }
     * @return JSON字符串
     */
    @RequestMapping(value ="/verify")
    @ResponseBody
    public String verify(@RequestBody JSONObject jsonObject) {
        JSONObject result = new JSONObject();
        //检查参数
        String signToken ;
        String sealId ;
        String signTime ;
        String hashValue ;
        byte[] signedValue ;
        if (jsonObject.containsKey("signToken")&&jsonObject.containsKey("sealId")&&jsonObject.containsKey("hashValue")
                &&jsonObject.containsKey("signedValue")){
            signToken = jsonObject.getString("signToken");
            sealId = jsonObject.getString("sealId");
            hashValue = jsonObject.getString("hashValue");
            signedValue = ESSGetBase64Decode(jsonObject.getString("signedValue"));
            signTime = jsonObject.getString("signTime");
            hashValue = hashValue +signTime;
        }else {
            result.put("code","w0001");
            result.put("msg","参数类型不全");
            return result.toJSONString();
        }
        SealDto seal = sealService.findSealById(sealId);
        if (seal==null){
            result.put("code","w0002");
            result.put("msg","印章不存在");
            return result.toJSONString();
        }
        String sealStan = seal.getSeal_standard();
        boolean documentVerify = false;
        if("4".equals(sealStan)||"5".equals(sealStan)||"6".equals(sealStan)){
            documentVerify =  webService.webDocumentVerifyRSA(hashValue.getBytes(),ESSGetBase64Decode(seal.getSeal_cert()),signedValue);
        }else{
            documentVerify =  webService.webDocumentVerifySM(hashValue.getBytes(),ESSGetBase64Decode(seal.getSeal_cert()),signedValue);
        }
        boolean certVerify =  webService.webCertVerify(ESSGetBase64Decode(seal.getSeal_cert()));
        JSONObject msgJS = new JSONObject();
        msgJS.put("documentVerify",documentVerify);
        msgJS.put("certVerify",certVerify);

        result.put("code","w0000");
        result.put("msg",msgJS.toJSONString());
        return result.toJSONString();
    }



    /**
     * 根据UserToken获取可用印章列表。
     *参数示例
     *     {"signToken":"634e33ad3ea6475cb6c89c1e783130b8","imgType":"1"}
     *     结果示例
     *     {
     *     "msg": [
     *                 {
     *                 "sealName": "余建华",
     *                 "hashA": "SM3",
     *                 "signA": "SM2",
     *                 "sealId": "039ad7fe963a449da49d204154cf77f2",
     *                 "isUK": "0",
     *                 "sealStruct": "国办标准",
     *                 "imgType": "gif"
     *                 },
     *                 {
     *                 "sealName": "刘大富",
     *                 "hashA": "SM3",
     *                 "signA": "SM2",
     *                 "sealId": "022e9c4f36c04c928cd264a62966aed1",
     *                 "isUK": "0",
     *                 "sealStruct": "安可标准",
     *                 "imgType": "gif"
     *                 }
     *             ],
     *     "code": "w0000"
     *     }
     * @return JSON字符串
     */
    @RequestMapping(value ="/getSealListBySignToken")
    @ResponseBody
    public String getSealListByUserToken(@RequestBody JSONObject jsonObject) {
        JSONObject result = new JSONObject();
        //检查参数
        String signToken ;
        String imgType ;
        if (jsonObject.containsKey("signToken")){
            signToken = jsonObject.getString("signToken");
            imgType = jsonObject.getString("imgType");
            if ("1".equals(imgType)){
                imgType = "gif";
            }else if ("2".equals(imgType)){
                imgType = "wacom";
            }
        }else {
            result.put("code","w0001");
            result.put("msg","参数类型不全");
            return result.toJSONString();
        }
        JSONArray msgJsonA = new JSONArray();
        //根据单位和部门查找印章列表
        PdfToken pdfToken = pdfTokenService.selectPdfTokenByTokenId(signToken);
        JSONArray jsonArray = JSONArray.parseArray(pdfToken.getSealList());
        for (Object o : jsonArray) {
            JSONObject sealJson = new JSONObject();
            //查找印章
            SealDto seal = sealService.findSealById((String) o);
            if (imgType.equals(seal.getSeal_img_type())){
                sealJson.put("sealName",seal.getSealName());
                sealJson.put("phoneNumber","");
                sealJson.put("sealId",seal.getSealId());
                sealJson.put("imgType",seal.getSeal_img_type());
                String sealStandard = seal.getSeal_standard();
                if(sealStandard.equals("1") || sealStandard.equals("2") || sealStandard.equals("3")){
                    sealJson.put("hashA","SM3");
//                    sealJson.put("signA","SM2");
                }else {
                    sealJson.put("hashA","SHA1");
//                    sealJson.put("signA","RSA");
                }
                if(sealStandard.equals("1") || sealStandard.equals("2") || sealStandard.equals("4")){
                    sealJson.put("isUK","1");
                }else {
                    sealJson.put("isUK","0");
                }
                if(sealStandard.equals("4") || sealStandard.equals("5") || sealStandard.equals("6")){
                    sealJson.put("sealStruct","公司自定义标准");
                }else if(sealStandard.equals("1") || sealStandard.equals("3") ){
                    sealJson.put("sealStruct","安可标准");
                }else if(sealStandard.equals("2")){
                    sealJson.put("sealStruct","国办标准");
                }
                msgJsonA.add(sealJson);
            }
        }
        result.put("code","w0000");
        result.put("msg",msgJsonA);
        return result.toJSONString();
    }
    /**
     * 根据UserToken获取可用印章列表。
     *参数示例
     *     {"signToken":"634e33ad3ea6475cb6c89c1e783130b8","sealId":"1"}
     *     结果示例
     *     {
     *          "msg": {
     *             "imgBase64 ": "",
     *             "imgWidth ": "",
     *             "imgHeight ": "",
     *             "imgType": ""
     *             }
     *          "code": "w0000"
     *      }
     *
     * @return JSON字符串
     */
    @RequestMapping(value ="/getSealImgBySealId")
    @ResponseBody
    public String getSealImgBySealId(@RequestBody JSONObject jsonObject) {
        JSONObject result = new JSONObject();
        //检查参数
        String signToken ;
        String sealId ;
        if (jsonObject.containsKey("signToken")){
            signToken = jsonObject.getString("signToken");
            sealId = jsonObject.getString("sealId");
        }else {
            result.put("code","w0001");
            result.put("msg","参数类型不全");
            return result.toJSONString();
        }
        JSONObject sealJson = new JSONObject();
        //查找印章
        SealDto seal = sealService.findSealById(sealId);
        if (!isNull(seal)){
            sealJson.put("imgType",seal.getSeal_img_type());
            sealJson.put("imgBase64",seal.getSeal_img());
            sealJson.put("imgWidth",seal.getSeal_w());
            sealJson.put("imgHeight",seal.getSeal_h());
            result.put("code","w0000");
            result.put("msg",sealJson);
            return result.toJSONString();
        }
        result.put("code","w0002");
        result.put("msg","未找到符合要求的数据");
        return result.toJSONString();
    }

    /**
     *     参数
     *         {
     *             "sysId":"SYS001",
     *             "userId":"00000189",
     *             "documentCode":"xxxxx"
     *         }
     *     返回结果
     *         {"msg":"xxxxx","code":"0000"}
     * @param data
     * @return
     */
    @RequestMapping(value="/getSignToken")
    @ResponseBody
    public String getSignToken(@RequestBody JSONObject data) {
        JSONObject result = new JSONObject();
//        //检查参数
        String sysId;
        String userId;
//        String documentCode;
        if (data.containsKey("sysId")&&data.containsKey("userId")){
            sysId = data.getString("sysId");
            userId = data.getString("userId");
//            documentCode = data.getString("documentCode");
        }else {
            result.put("code","w0001");
            result.put("msg","参数类型不全");
            return result.toJSONString();
        }
        //根据业务系统和人员查找授权列表
        List<SealJur> sealJurs = sealJurService.selectAllSealJurByUserId(userId,sysId);
        List<SealDto> sealList = new ArrayList<>();
        for (SealJur s:sealJurs) {
            SealDto sealDto = sealService.findSealById(s.getSealId());
            if(!isNull(sealDto)){
                sealList.add(sealDto);
            }
        }

        if (sealList.size() == 0){
            result.put("code","w0002");
            result.put("msg","用户ID错误");
            return result.toJSONString();
        }
        JSONArray ja = new JSONArray();
        for (SealDto sealDto: sealList) {
            ja.add(sealDto.getSealId());
        }

        PdfToken pdfToken = new PdfToken();
        pdfToken.setTokenId(IdUtils.simpleUUID());
        pdfToken.setSysId(sysId);
        pdfToken.setUserId(userId);
//        pdfToken.setDocumentId(documentCode);
        pdfToken.setSealList(ja.toJSONString());
        pdfToken.setState(1);
        pdfToken.setInputTime(DateUtils.getTime());
        pdfToken.setSealPassword("123456");
        int res = pdfTokenService.insertPdfToken(pdfToken);
        //将data数据存入数据库中，
        result.put("code","w0000");
        result.put("msg",pdfToken.getTokenId());
        return result.toJSONString();
    }

//    /**
//     * 获取UserToken签章凭证。
//     *     参数示例
//     *     {"sysId":"SYS001","userId":"00000189"}
//     *     结果示例
//     *     {
//     *         "msg": "634e33ad3ea6475cb6c89c1e783130b8",
//     *         "code": "w0000"
//     *     }
//     * @return JSON字符串
//     */
//    @RequestMapping(value ="/getSignToken")
//    @ResponseBody
//    public String getSignToken(@RequestBody JSONObject jsonObject) {
//        JSONObject result = new JSONObject();
////        //检查参数
//        String sysId;
//        String userId;
//        if (jsonObject.containsKey("sysId")&&jsonObject.containsKey("userId")){
//            sysId = jsonObject.getString("sysId");
//            userId = jsonObject.getString("userId");
//        }else {
//            result.put("code","w0001");
//            result.put("msg","参数类型不全");
//            return result.toJSONString();
//        }
//        String signToken = IdUtils.simpleUUID();
//
//        //根据业务系统和人员查找授权列表
//        List<SealJur> sealJurs = sealJurService.selectAllSealJurByUserId(userId,sysId);
//        List<SealDto> sealList = new ArrayList<>();
//        for (SealJur s:sealJurs) {
//            SealDto sealDto = sealService.findSealById(s.getSealId());
//            if(!isNull(sealDto)){
//                sealList.add(sealDto);
//            }
//        }
//        if (sealList.size() == 0){
//            result.put("code","w0002");
//            result.put("msg","用户ID错误");
//            return result.toJSONString();
//        }
//        for (SealDto sealDto: sealList) {
//            WebToken webToken = new WebToken();
//            webToken.setTokenId(signToken);
//            webToken.setSealId(sealDto.getSealId());
//            webToken.setSysId(sysId);
//            webToken.setState(1);
//            webToken.setInputTime(DateUtils.getTime());
//            webService.addWebToken(webToken);
//        }
//        //将data数据存入数据库中，
//        result.put("code","w0000");
//        result.put("msg",signToken);
//        return result.toJSONString();
//    }
//
//    /**
//     * 根据UserToken获取可用印章列表。
//     *参数示例
//     *     {"signToken":"634e33ad3ea6475cb6c89c1e783130b8","imgType":"1"}
//     *     结果示例
//     *     {
//     *     "msg": [
//     *                 {
//     *                 "sealName": "余建华",
//     *                 "hashA": "SM3",
//     *                 "signA": "SM2",
//     *                 "sealId": "039ad7fe963a449da49d204154cf77f2",
//     *                 "isUK": "0",
//     *                 "sealStruct": "国办标准",
//     *                 "imgType": "gif"
//     *                 },
//     *                 {
//     *                 "sealName": "刘大富",
//     *                 "hashA": "SM3",
//     *                 "signA": "SM2",
//     *                 "sealId": "022e9c4f36c04c928cd264a62966aed1",
//     *                 "isUK": "0",
//     *                 "sealStruct": "安可标准",
//     *                 "imgType": "gif"
//     *                 }
//     *             ],
//     *     "code": "w0000"
//     *     }
//     * @return JSON字符串
//     */
//    @RequestMapping(value ="/getSealListBySignToken")
//    @ResponseBody
//    public String getSealListByUserToken(@RequestBody JSONObject jsonObject) {
//        JSONObject result = new JSONObject();
//        //检查参数
//        String signToken ;
//        String imgType ;
//        if (jsonObject.containsKey("signToken")){
//            signToken = jsonObject.getString("signToken");
//            imgType = jsonObject.getString("imgType");
//            if ("1".equals(imgType)){
//                imgType = "gif";
//            }else if ("2".equals(imgType)){
//                imgType = "wacom";
//            }
//        }else {
//            result.put("code","w0001");
//            result.put("msg","参数类型不全");
//            return result.toJSONString();
//        }
//        JSONArray msgJsonA = new JSONArray();
//        //根据单位和部门查找印章列表
//        List<String> sealList = webService.findSealListByToken(signToken);
//        for (String sealId:sealList) {
//            JSONObject sealJson = new JSONObject();
//            //查找印章
//            SealDto seal = sealService.findSealById(sealId);
//            if (imgType.equals(seal.getSeal_img_type())){
//                sealJson.put("sealName",seal.getSealName());
//                sealJson.put("sealId",seal.getSealId());
//                sealJson.put("imgType",seal.getSeal_img_type());
//                String sealStandard = seal.getSeal_standard();
//
//                if(sealStandard.equals("1") || sealStandard.equals("2") || sealStandard.equals("3")){
//                    sealJson.put("hashA","SM3");
////                    sealJson.put("signA","SM2");
//                }else {
//                    sealJson.put("hashA","SHA1");
////                    sealJson.put("signA","RSA");
//                }
//                if(sealStandard.equals("1") || sealStandard.equals("2") || sealStandard.equals("4")){
//                    sealJson.put("isUK","1");
//                }else {
//                    sealJson.put("isUK","0");
//                }
//                if(sealStandard.equals("4") || sealStandard.equals("5") || sealStandard.equals("6")){
//                    sealJson.put("sealStruct","公司自定义标准");
//                }else if(sealStandard.equals("1") || sealStandard.equals("3") ){
//                    sealJson.put("sealStruct","安可标准");
//                }else if(sealStandard.equals("2")){
//                    sealJson.put("sealStruct","国办标准");
//                }
//                msgJsonA.add(sealJson);
//            }
//        }
//        result.put("code","w0000");
//        result.put("msg",msgJsonA);
//        return result.toJSONString();
//    }
//    /**
//     * 根据UserToken获取可用印章列表。
//     *参数示例
//     *     {"signToken":"634e33ad3ea6475cb6c89c1e783130b8","sealId":"1"}
//     *     结果示例
//     *     {
//     *          "msg": {
//     *             "imgBase64 ": "",
//     *             "imgWidth ": "",
//     *             "imgHeight ": "",
//     *             "imgType": ""
//     *             }
//     *          "code": "w0000"
//     *      }
//     *
//     * @return JSON字符串
//     */
//    @RequestMapping(value ="/getSealImgBySealId")
//    @ResponseBody
//    public String getSealImgBySealId(@RequestBody JSONObject jsonObject) {
//        JSONObject result = new JSONObject();
//        //检查参数
//        String signToken ;
//        String sealId ;
//        if (jsonObject.containsKey("signToken")){
//            signToken = jsonObject.getString("signToken");
//            sealId = jsonObject.getString("sealId");
//        }else {
//            result.put("code","w0001");
//            result.put("msg","参数类型不全");
//            return result.toJSONString();
//        }
//        JSONObject sealJson = new JSONObject();
//        //根据单位和部门查找印章列表
//        List<String> sealList = webService.findSealListByToken(signToken);
//        for (String sealIdN:sealList) {
//            if (sealIdN.equals(sealId)){
//                //查找印章
//                SealDto seal = sealService.findSealById(sealId);
//                sealJson.put("imgType",seal.getSeal_img_type());
//                sealJson.put("imgBase64",seal.getSeal_img());
//                sealJson.put("imgWidth",seal.getSeal_w());
//                sealJson.put("imgHeight",seal.getSeal_h());
//                result.put("code","w0000");
//                result.put("msg",sealJson);
//                return result.toJSONString();
//            }
//        }
//        result.put("code","w0002");
//        result.put("msg","未找到符合要求的数据");
//        return result.toJSONString();
//    }
    /**
     * 保存印章
     *     参数示例
     *     {
     *         "signToken":"634e33ad3ea6475cb6c89c1e783130b8",
     *         "documentId":"022e9c4f36c04c928cd264a62966aed1",
     *         "approvalProcessNumber":"1",
     *         "sealList":[
     *             {
     *                 "sealId":"022e9c4f36c04c928cd264a62966aed1",
     *                 "sealContent":"XXXXXXX"
     *             }
     *         ]
     *     }
     *     结果示例
     *     {
     *         "msg": "保存成功",
     *         "code": "w0000"
     *     }
     * @return JSON字符串
     */
    @RequestMapping(value ="/saveSeal")
    @ResponseBody
    public String saveSeal(@RequestBody JSONObject jsonObject) {
        JSONObject result = new JSONObject();
        //检查参数
        String signToken;
        String documentId ;
        String approvalProcessNumber ;
        JSONArray sealList;
        if (jsonObject.containsKey("signToken")&&jsonObject.containsKey("documentId")&&jsonObject.containsKey("approvalProcessNumber")
                &&jsonObject.containsKey("sealList")){
            signToken = jsonObject.getString("signToken");
            documentId = jsonObject.getString("documentId");
            approvalProcessNumber = jsonObject.getString("approvalProcessNumber");
            sealList = jsonObject.getJSONArray("sealList");
        }else {
            result.put("code","w0001");
            result.put("msg","参数类型不全");
            return result.toJSONString();
        }
        //插入新数据
        for (int i = 0; i < sealList.size(); i++) {
            JSONObject sealJs =  sealList.getJSONObject(i);
            String sealId = sealJs.getString("sealId");
            //SerializerFeature.WriteMapNullValue  使用输出为null的参数，默认为false
            String sealContent = JSON.toJSONString(sealJs, SerializerFeature.WriteMapNullValue);
            String sealToken = sealJs.getString("sealToken");
            WebDocument webDocument = new WebDocument();
            webDocument.setApprovalProcessNumber(approvalProcessNumber);
            webDocument.setSealContent(sealContent);
            webDocument.setDocumentId(documentId);
            webDocument.setSealId(sealId);
            webDocument.setSealToken(sealToken);
            boolean deleteResult = webService.deleteWebDocument(webDocument);
            boolean saveResult = webService.addWebDocument(webDocument);
            if (!saveResult){
                result.put("code","w0002");
                result.put("msg","保存失败");
                return result.toJSONString();
            }
        }
        result.put("code","w0000");
        result.put("msg","保存成功");
        return result.toJSONString();
    }
    /**
     * 查找印章
     *     参数示例
     *     {
     *         "signToken":"634e33ad3ea6475cb6c89c1e783130b8",
     *         "documentId":"022e9c4f36c04c928cd264a62966aed1",
     *         "approvalProcessNumber":"1"
     *     }
     *     结果示例
     *     {
     *     "msg": {
     *                 "022e9c4f36c04c928cd264a62966aed1": "XXXXXXX"
     *             },
     *     "code": "w0000"
     *     }
     * @return JSON字符串
     */
    @RequestMapping(value ="/getSealList")
    @ResponseBody
    public String getSealList(@RequestBody JSONObject jsonObject) {
        JSONObject result = new JSONObject();
        //检查参数
        String signToken;
        String documentId;
        String approvalProcessNumber ;
        if (jsonObject.containsKey("signToken")&&jsonObject.containsKey("documentId")&&jsonObject.containsKey("approvalProcessNumber")
              ){
            signToken = jsonObject.getString("signToken");
            documentId = jsonObject.getString("documentId");
            approvalProcessNumber = jsonObject.getString("approvalProcessNumber");
        }else {
            result.put("code","w0001");
            result.put("msg","参数类型不全");
            return result.toJSONString();
        }
        JSONArray jsonArray = new JSONArray();
        List<WebDocument> webDocumentList = webService.findWebDocument(documentId,approvalProcessNumber);
        for (WebDocument webDocument:webDocumentList) {
            JSONObject seal = new JSONObject();
            seal.put("sealContent",webDocument.getSealContent());
            seal.put("sealId",webDocument.getSealId());
            jsonArray.add(seal);
        }
        result.put("code","w0000");
        result.put("msg",jsonArray);
        return result.toJSONString();
    }

    /**
     * 删除印章
     *     参数示例
     *     {
     *         "sealToken":"634e33ad3ea6475cb6c89c1e783130b8",
     *         "documentId":"022e9c4f36c04c928cd264a62966aed1",
     *         "sealToken":"022e9c4f36c04c928cd264a62966aed1",
     *         "approvalProcessNumber":"1"
     *     }
     *     结果示例
     *     {
     *     "msg": "删除成功",
     *     "code": "w0000"
     *     }
     * @return JSON字符串
     */
    @RequestMapping(value ="/deleteSeal")
    @ResponseBody
    public String deleteSeal(@RequestBody JSONObject jsonObject) {
        JSONObject result = new JSONObject();
        //检查参数
        String signToken;
        String documentId;
        String approvalProcessNumber;
        String sealToken;
        String verifyUUID;
        if (jsonObject.containsKey("signToken")&&jsonObject.containsKey("documentId")&&jsonObject.containsKey("approvalProcessNumber")
                &&jsonObject.containsKey("sealToken")){
            signToken = jsonObject.getString("signToken");
            documentId = jsonObject.getString("documentId");
            approvalProcessNumber = jsonObject.getString("approvalProcessNumber");
            sealToken = jsonObject.getString("sealToken");
//            verifyUUID = jsonObject.getString("verifyUUID");
        }else {
            result.put("code","w0001");
            result.put("msg","参数类型不全");
            return result.toJSONString();
        }

//        if (!pdfTokenService.checkVerifyUUID(signToken,verifyUUID)){
//            result.put("code","w0005");
//            result.put("msg","当前操作未验证，请先验证");
//            return result.toJSONString();
//        }

        WebDocument webDocument = new WebDocument();
        webDocument.setDocumentId(documentId);
        webDocument.setApprovalProcessNumber(approvalProcessNumber);
        webDocument.setSealToken(sealToken);
        boolean saveResult = webService.deleteWebDocument(webDocument);
        if (saveResult){
            result.put("code","w0000");
            result.put("msg","删除成功");
        }else {
            result.put("code","w0002");
            result.put("msg","删除失败");
        }
        return result.toJSONString();
    }

    /**
     * 证书解析接口
     *     参数示例
     *         {
     *         "certBase64":"xxxxxxxx"
     *         }
     *     结果示例
     *         {
     *             "msg":{
     *                 "sIssuerInfo":"C=CN,O=Tobacco,CN=重庆中烟CA",
     *                 "sTimeEnd":"2031年1月18日",
     *                 "sTimeBegin":"2021年1月20日",
     *                 "sSignAlgName":"SM3WITHSM2",
     *                 "sUserInfo":"C=CN,O=Tobacco,OU=重庆中烟工业有限责任公司,OU=重庆中烟本部,OU=公司领导,CN=程晓苏",
     *                 "sCertNo":"43971079453"
     *             },
     *             "code":"w0000"
     *         }
     * @return JSON字符串
     */
    @RequestMapping(value ="/GetCertInfo")
    @ResponseBody
    public String GetCertInfo(@RequestBody JSONObject jsonObject) {
        JSONObject result = new JSONObject();
        //检查参数
        String certBase64;
        if (jsonObject.containsKey("certBase64")){
            certBase64 = jsonObject.getString("certBase64");
        }else {
            result.put("code","w0001");
            result.put("msg","参数类型不全");
            return result.toJSONString();
        }
        CertificateInfo certificateInfo =  ESSCertificate.GetCertInfo(ESSGetBase64Decode(certBase64));
//        CertificateInfo certificateInfo =  GetCertInfoFromCer(ESSGetBase64Decode(certBase64));
        if (certificateInfo.sCertNo==null){
            result.put("code","w0001");
            result.put("msg","解析异常");
        }
        JSONObject msgJson = new JSONObject();
        msgJson.put("sCertNo",certificateInfo.sCertNo);
        msgJson.put("sSignAlgName",certificateInfo.sSignAlgName);
        msgJson.put("sIssuerInfo",certificateInfo.sIssuerInfo);
        msgJson.put("sUserInfo",certificateInfo.sUserInfo);
        msgJson.put("sTimeBegin",certificateInfo.sTimeBegin);
        msgJson.put("sTimeEnd",certificateInfo.sTimeEnd);

        result.put("code","w0000");
        result.put("msg",msgJson);

        return result.toJSONString();
    }
    /**
     * 对接收的参数做一次补全，目的是支持两种参数形式
     * Content-Type: application/x-www-form-urlencoded  参数正文
     * Content-Type: application/json 文本正文
     * @return 参数
     */
    public String complementParam(String data,HttpServletRequest request){
        if (data ==null){
            StringBuilder data1 = new StringBuilder();
            String line ;
            BufferedReader reader ;
            try {
                reader = request.getReader();
                while (null != (line = reader.readLine())) {
                    data1.append(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            data = data1.toString();
        }
        return data;
    }
}
